using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using xiushen.Domain;

namespace xiushen.Tests
{
    public abstract  class BaseVideoDownLoad
    {
        protected string a_pattern { get; set; }
        protected string a_title { get; set; }
        protected string imgpattern { get; set; }
        protected XiushenRepository _repository = new XiushenRepository();
        protected string checkedLineValue { get; set; }
        protected string videoLineValue { get; set; }
        protected string VideoSource { get; set; }
        public int CategoryID { get; set; }
        protected BaseVideoDownLoad()
        {
            a_pattern = @"(?<=<a[^<>]+href=""?)[^""<>]+(?=""?[^<>]*>)";
            a_title = @"(?<=<a+[^<>]+>)[^""<>]+(?=[</a>])";
            checkedLineValue = "	<ul class=\"video\">";
            imgpattern = @"<img [^>]+>";
            VideoSource = "youku";
            videoLineValue = "<li class=\"videoImg\">";
            CategoryID = 78;
        }
        public void UpdateTwoLines(string firstPostionLine, string videoLine)
        {
            checkedLineValue = firstPostionLine;
            videoLineValue = videoLine;
        }
        public void DownLoadAllVideos(string url)
        {

            IList<Domain.Videos> videoses = DownLoadVideoList(url);

            InsertVidoesToDB(CategoryID, videoses);
        }
        public void DownLoadAllVideosWithLinks(string url)
        {
            var vidourlLists = GetVideoURLList(url);
            IList<Domain.Videos> videoses = new List<Videos>();

            foreach (var vidourlList in vidourlLists)
            {
                videoses.Add(DownLoadRealContent(vidourlList));
            }
            InsertVidoesToDB(CategoryID, videoses);
        }

        private void InsertVidoesToDB(int categoryId,IList<Domain.Videos> videos)
        {
            foreach (var videose in videos)
            {
                videose.CategoryID = categoryId;
                _repository.Insert(videose);
            }
        }
        public virtual IList<Domain.Videos> DownLoadVideoList(string urls)
        {
            var url = new Uri(urls);
            var content = Spider.getHTML3(url);
           
            StreamReader reader = new StreamReader(new MemoryStream(content), Encoding.UTF8);
            var urlList = new List<string>();
            IList<Domain.Videos> videoses = new List<Videos>();

            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                if (line.Contains(checkedLineValue))
                {
                    while (!reader.EndOfStream)
                    {

                        var newLine = reader.ReadLine();
                        if (newLine.Contains(videoLineValue))
                        {
                            videoses.Add(GetVideo(newLine));
                        }
                    }
                    break;

                }

            }
            return videoses;

        }

     
        protected virtual Domain.Videos GetVideo( string line)
        {
            var video = new Domain.Videos();
            video.SourceSite = VideoSource;
            var result = Regex.Match(line, imgpattern, RegexOptions.IgnoreCase);
            video.PicURL = GetValuesBetween(result.Value, "src");
            video.Name = GetValuesBetween(result.Value, "title");
            video.SourceFile = GetValuesBetween(result.Value, "name");
            return video;
        }

        protected virtual IList<string> GetVideoURLList(string url){return new List<string>();} 

        public virtual Domain.Videos DownLoadRealContent(string url)
        {
            var pattern = @"<script[^>]*?>(\w|\W)*?</script>";
            var metapattern = @"<meta name[^>]+>";
            var content = Spider.getHTML(new Uri(url));
            var result = Regex.Matches(content, pattern, RegexOptions.IgnoreCase);
            var result2 = Regex.Matches(content, metapattern, RegexOptions.IgnoreCase);
            var video = new Domain.Videos();
            foreach (Match match in result2)
            {
                if (match.Value.Contains("title"))
                {
                    video.Name = GetSub2(match.Value, "content");
                }

                if (match.Value.Contains("description"))
                {
                    video.Description = GetSub2(match.Value, "content");
                }
            }
            foreach (Match match in result)
            {
                if (match.Value.Contains("videoId2"))
                {

                    video.SourceSite = "youku";

                    var values = match.Value.Split(';');
                    foreach (var value in values)
                    {
                        if (value.Contains("videoId2"))
                        {
                            video.SourceFile = GetSub(value);
                        }

                    }


                }
            }
            return video;



        }


        protected string GetSub(string value, string passvalue = "'")
        {
            var data = value.IndexOf(passvalue);
            return value.Substring(data + 1, value.Length - data - 4);
        }

        protected string GetSub2(string value, string passvalue = "'")
        {
            var startindex = value.IndexOf(passvalue);
            var index = value.IndexOf("\"", startindex);
            return value.Substring(index + 1, value.Length - index - 4);
        }
        protected string GetValuesBetween(string value, string passvalues)
        {
            var startindex = value.IndexOf(passvalues);
            var index = value.IndexOf("\"", startindex);
            var nextIndex = value.IndexOf("\"", index + 1);
            return value.Substring(index + 1, nextIndex - index - 1);
        }
    }
}